package dao;

import dao.imp.IRewardsDao;
import entity.Rewards;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import util.MysqlConnUtil;

import java.util.List;

/**
 * @program: personnel_management_system
 * @description: rewards opt
 * @author: Mr.Sun
 * @create: 2020-05-25 14:59
 **/

public class RewardsDao implements IRewardsDao {
    private JdbcTemplate template = new JdbcTemplate(MysqlConnUtil.getDataSource());

    public RewardsDao() throws Exception {
    }

    @Override
    public boolean insert(Rewards rewards) {
        String sql = "insert into rewards(type,date,emp_id) values(?,?,?)";
        Integer count = template.update(sql,rewards.getType(),rewards.getDate(),rewards.getEmp_id());
        return count ==1;
    }

    @Override
    public boolean update(Rewards rewards) {
        boolean res = false;
        String sql = "UPDATE rewards SET type = ？, date = ? , emp_id = ? WHERE r_id = ?";

        int count = template.update(sql,rewards.getType(),rewards.getDate(),rewards.getEmp_id(),rewards.getR_id());
        res = count == 1;
        return res;
    }

    @Override
    public boolean delete(Rewards rewards) {
        boolean res =false;
        String sql = "DELETE FROM rewards WHERE r_id = ?";
        int count = template.update(sql,rewards.getR_id());
        return count == 1;
    }

    @Override
    public List<Rewards> selectAll() {
        String sql = "select * from rewards";
        List<Rewards> rewards = template.query(sql, new BeanPropertyRowMapper<Rewards>(Rewards.class));
        System.out.println(rewards);
        return rewards;
    }

    @Override
    public List<Rewards> select(Rewards rewards) {
        return null;
    }
}
